library(plyr)
library(dplyr)
library(tidyverse)
library(gsynth)
library(stargazer)
library(modelsummary) # https://modelsummary.com/articles/modelsummary.html#new-models-and-custom-statistics

setwd("~/Dropbox/China Huawei Paper/Production Materials/Replication/")
master.data <- read.csv("dataFile_China_Huawei.csv"); head(master.data)

bmr = read.csv('political-regime-bmr.csv');head(bmr)
colnames(bmr)=c("country_name","code2","year","bmr_regime")

m2 = join(master.data,bmr,by=c("country_name","year"));nrow(master.data);nrow(m2)
master.data=m2


# 250k
dat <- master.data
dat$treated = 0
threshold=250000 #, good
dat = as.data.frame(dat %>%
                      group_by(cabb) %>%
                      dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
                                    treated = ifelse(china.huawei > threshold, 1, treated)) %>%
                      fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$bmr_regime==1,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out1$est.avg
out1est250 = out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out2$est.avg
out2est250 = out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out3$est.avg
out3est250 = out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out4$est.avg
out4est250 = out4$est.avg

# 500k
dat <- master.data
dat$treated = 0
threshold=500000 #, good
dat = as.data.frame(dat %>%
                      group_by(cabb) %>%
                      dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
                                    treated = ifelse(china.huawei > threshold, 1, treated)) %>%
                      fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$bmr_regime==1,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out1$est.avg
out1est500 = out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out2$est.avg
out2est500 = out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out3$est.avg
out3est500 = out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out4est500 = out4$est.avg

# 1 million
dat <- master.data
dat$treated = 0
threshold=1000000
dat = as.data.frame(dat %>%
                      group_by(cabb) %>%
                      dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
                                    treated = ifelse(china.huawei > threshold, 1, treated)) %>%
                      fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$bmr_regime==1,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out1$est.avg
out1est1m = out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out2$est.avg
out2est1m = out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out3$est.avg
out3est1m = out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out4$est.avg
out4est1m = out4$est.avg


# 5 million
dat <- master.data
dat$treated = 0
threshold=5000000
dat = as.data.frame(dat %>%
                      group_by(cabb) %>%
                      dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
                                    treated = ifelse(china.huawei > threshold, 1, treated)) %>%
                      fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$bmr_regime==1,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=4, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out1$est.avg
out1est5m = out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=4, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out2$est.avg
out2est5m = out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=4, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out3$est.avg
out3est5m = out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=4, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out4$est.avg
out4est5m = out4$est.avg

# 10 million
dat <- master.data
dat$treated = 0
threshold=10000000
dat = as.data.frame(dat %>%
                      group_by(cabb) %>%
                      dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
                                    treated = ifelse(china.huawei > threshold, 1, treated)) %>%
                      fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$bmr_regime==1,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=4, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out1$est.avg
out1est10m = out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=4, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out2$est.avg
out2est10m = out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=4, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out3$est.avg
out3est10m = out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=4, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out4$est.avg
out4est10m = out4$est.avg


# export table
ti = data.frame(rbind(out1est250,out1est500,out1est1m,out1est5m,out1est10m))
ti$term = c("Transfer \u2265 $250,000","Transfer \u2265 $500,000","Transfer \u2265 $1,000,000","Transfer \u2265 $5,000,000","Transfer \u2265 $10,000,000")
ti = ti[,c("term","Estimate","S.E.","p.value")]
names(ti) = c("term","estimate","std.error","p.value")
out1 = list(tidy=ti)
class(out1) = "modelsummary_list"

ti = data.frame(rbind(out2est250,out2est500,out2est1m,out2est5m,out2est10m))
ti$term = c("Transfer \u2265 $250,000","Transfer \u2265 $500,000","Transfer \u2265 $1,000,000","Transfer \u2265 $5,000,000","Transfer \u2265 $10,000,000")
ti = ti[,c("term","Estimate","S.E.","p.value")]
names(ti) = c("term","estimate","std.error","p.value")
out2 = list(tidy=ti)
class(out2) = "modelsummary_list"

ti = data.frame(rbind(out3est250,out3est500,out3est1m,out3est5m,out3est10m))
ti$term = c("Transfer \u2265 $250,000","Transfer \u2265 $500,000","Transfer \u2265 $1,000,000","Transfer \u2265 $5,000,000","Transfer \u2265 $10,000,000")
ti = ti[,c("term","Estimate","S.E.","p.value")]
names(ti) = c("term","estimate","std.error","p.value")
out3 = list(tidy=ti)
class(out3) = "modelsummary_list"

ti = data.frame(rbind(out4est250,out4est500,out4est1m,out4est5m,out4est10m))
ti$term = c("Transfer \u2265 $250,000","Transfer \u2265 $500,000","Transfer \u2265 $1,000,000","Transfer \u2265 $5,000,000","Transfer \u2265 $10,000,000")
ti = ti[,c("term","Estimate","S.E.","p.value")]
names(ti) = c("term","estimate","std.error","p.value")
out4 = list(tidy=ti)
class(out4) = "modelsummary_list"



myrows = data.frame(list(c("Country Fixed Effects",rep("Yes",4))
                         ,c("Year Fixed Effects",rep("Yes",4))
                         ,c("Control Variables",rep("Yes",4))
))

export = modelsummary(list("Internet Filtering" = out1
                           , "Internet Shutdowns" = out2
                           ,"Social Media Monitoring"=out3
                           ,"Arrests for Political Content"=out4
)
,add_rows=data.frame(t(myrows))
, stars=c('*' = .1, '**' = 0.05, '***' = .01)
, output="latex_tabular"
)
export = gsub("≥", "$\\\\ge$", export)
export
